<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employees"
            :key="item.key"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getEmployeeSimple,
  addDepartments,
  getDepartDetail,
  updateDepartments
} from '@/api/department.js'
export default {
  name: 'DeptDialog',
  props: {
    parentId: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    originList: {
      type: Array,
      required: true
    }
  },

  data() {
    // 部门编码重复验证
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map((item) => item.code) // originList是父级元素传过来的所有的数据,把里面code删选出来,放在一个数组中
      if (this.isEdit) {
        existCodeList = this.originList
          .filter((item) =>
            item.id !== this.parentId
          )
          .map((item) => item.code)
      }

      existCodeList.includes(value)
        ? callback(new Error('编码' + value + '已经存在'))
        : callback()
      // val是表单中传入的数据     如果检测到数据中有输入的值抛出已经存在   如果没有放行
    } // existCodeList存放所有数据中的code

    // 部门名称重复验证
    const validName = (rule, value, callback) => {
      let existNameList = this.originList
        .filter(item => item.pid === this.parentId)
        .map(item => item.name)
      if (this.isEdit) {
        const dept = this.originList.find(item => item.id === this.parentId)
        const pid = dept.pid
        existNameList = this.originList
          .filter(item => item.pid === pid && item.id !== this.parentId)
          .map(item => item.name)
      }
      existNameList.includes(value)
        ? callback(new Error('部门名称' + value + '已经存在'))
        : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [], // 员工数据下拉列表中
      //   表单验证规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getEmployeeData() // 获取下拉员工列表接口
    this.getDepartDetail() // 获取用户信息用来回填接口
  },
  methods: {
    // 获取部门信息用来回填
    async getDepartDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.parentId)
        this.form = res.data
      }
    },
    // 添加部门
    async addPeptHandler() {
      await addDepartments({ ...this.form, pid: this.parentId })
      this.$message.success('添加部门成功')
      this.$emit('update-depart')
    },
    // 编辑部门
    async editPeptHandler() {
      await updateDepartments(this.parentId, this.form)
      this.$message.success('编辑部门成功')
      this.$emit('update-depart')
    },
    // 添加编辑确认按钮-如果状态是添加调用上面的添加方法,如果是编辑调用编辑方法
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.editPeptHandler() : this.addPeptHandler()
        }
      })
    },

    // 取消按钮,点击取消,弹窗关闭
    hCancel() {
      this.$emit('close-dialog')
    },

    // 获取下拉员工列表
    async getEmployeeData() {
      const res = await getEmployeeSimple()
      this.employees = res.data
    }
  }
}
</script>

<style>
.element.style {
  padding: 0 30px 0 20px;
}
</style>
